package com.qzh.G.base;

import cn.hutool.core.util.IdUtil;
import com.qzh.G.utils.RandomUtil;

import java.math.BigDecimal;
import java.util.List;

public class Enemy extends ActiveObject{
    //当前使用的武器类型
    private L1Type usedl1Type;
    private L2Type usedl2Type;
    private L3Type usedl3Type;
    private final EnemyType enemyType;
    Enemy(EnemyType enemyType,long id) {
        super(id);
        this.enemyType = enemyType;
    }

    @Override
    public Attack SingleAttack(Long id) {
        Attack attack = new Attack();
        attack.setFrom(this.getId());
        attack.setTo(id);
        attack.setUuid(IdUtil.fastUUID());
        attack.setDamageType1(usedl1Type);
        attack.setDamageType2(usedl2Type);
        attack.setDamageType3(usedl3Type);
        attack.setChr(getChr());
        attack.setChd(getChd());
        BigDecimal damage = getAttack();
        //乘以伤害增幅
        attack.setValue( damage.multiply(BigDecimal.ONE.add(getDamageRate())));
        return attack;
    }

    @Override
    public List<Attack> MultiAttack(List[] ids) {
        return super.MultiAttack(ids);
    }

    @Override
    public AttackResult getHurt(Attack attack) {
        boolean isDodge = RandomUtil.ask(getDodge_rate());
        boolean isCH = RandomUtil.ask(attack.getChr());
        BigDecimal damage=new BigDecimal(0);
        if(attack.getAttackType().equals(AttackType.heal)){
            BigDecimal newHp = this.getHp().add(attack.getValue());
            if(newHp.compareTo(getHpLimit())>=0){
                this.setHp(getHpLimit());
            }
            else {
                this.setHp(newHp);
            }

        }
        else{
            if(!isDodge){
                //攻击减防御
                damage = attack.getValue().subtract(getDefend());
                //计算伤害减幅
                damage = damage.multiply(BigDecimal.ONE.subtract(getReduceRate()));
                if(damage.compareTo(BigDecimal.ZERO)<=0){//如果伤害低于0，则设置为0
                    damage =   BigDecimal.ZERO;
                }
                if(isCH){
                    damage = attack.getChd().multiply(damage);
                }
            }
            BigDecimal newHp = this.getHp().subtract(damage);
            if(newHp.compareTo(BigDecimal.ZERO)<=0){
                death();
            }
            else {
                setHp(newHp);
            }

        }

        AttackResult attackResult = new AttackResult();
        if(isDodge){
            attackResult.setType(AttackResultEnum.MISS);
        }
        if(!isDodge){
            attackResult.setType(AttackResultEnum.SUCCESS);
        }
        attackResult.set_C_H(isCH);
        attackResult.setValue(damage);
        attackResult.setTargetHp(getHp());
        return attackResult;
    }
    void death() {
       ActiveObjectContainer.removeObject(getId());
    }
}
